package com.example.wlzz.aspect;

import com.example.wlzz.constant.CookiesConstant;
import com.example.wlzz.constant.RedisConstant;
import com.example.wlzz.dto.admin.RoleDTO;
import com.example.wlzz.entity.admin.Permission;
import com.example.wlzz.enums.AdminEnum;
import com.example.wlzz.enums.ResultEnum;
import com.example.wlzz.repository.RoleWithPermissionRepository;
import com.example.wlzz.repository.mapper.admin.RoleMapper;
import com.example.wlzz.utils.CookieUtil;
import com.example.wlzz.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Aspect
@Component
@Slf4j
@Order(value = 2)
public class ManagerPageAspect {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RoleWithPermissionRepository roleWithPermissionRepository;
    @Autowired
    private RoleMapper roleMapper;
    @Around("execution(* com.example.wlzz.controller.admin.AdminForPage.*(..))")
    public ModelAndView handleControllerMethod(ProceedingJoinPoint pjp)throws Throwable{
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //  System.err.println(request.getServletPath());
        Cookie cookie  = CookieUtil.get(request, CookiesConstant.ADMIN_TOKEN);
        if(cookie==null){
            log.error("【切面登陆校验】cookie不存在");
            return new ModelAndView("/login");
        }
        //去redis查询
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.ADMIN_TOKEN_PREFIX, cookie.getValue()));
        if (StringUtils.isEmpty(tokenValue)) {  //查询redis
            log.error("【切面登陆校验】Redis中查不到token");
            return new ModelAndView("/login");
        }
        RoleDTO roleDTO = roleMapper.getRoleName(tokenValue);
        if(roleDTO==null){
            return new ModelAndView("/login");
        }
        boolean flag = false;
        if(roleDTO.getId()==1){
            return new ModelAndView("/login");
        } else if(roleDTO.getId()==3) {
            String path = "/wlzz" + request.getServletPath();
            List<Permission> permissionList = roleMapper.findPermissionByRoleId(roleDTO.getId());
            for (Permission permission : permissionList) {
                if (permission.getUrl().equalsIgnoreCase(path)) {
                    flag = true;
                }
            }
            if (!flag) {
                return new ModelAndView("/login");
            }
        }
        Object object = pjp.proceed();  //拿到方法返回值的对象
        return (ModelAndView)object;
    }
}
